package com.coocaa.ops.admin.v1.onclickinfo.service;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.onclickinfo.biz.OnclickInfoBizImpl;
import com.coocaa.ops.admin.v1.onclickinfo.entity.CommonOnclickInfoEntity;
import com.coocaa.ops.admin.v1.onclickinfo.model.CommonOnclickInfoModel;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.mybatis.UpdateTemplate;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author bijiahao
 * @date : 2018/6/19.
 * @description: 点击事件-查询服务接口实现
 */
@Service
public class OnclickInfoServiceImpl implements IOnclickInfoService {
    @Autowired
    private OnclickInfoBizImpl onclickInfoBiz;
    private static CommonLanguageProperties commonLanguageProperties;

    @Override
    public void save(CommonOnclickInfoEntity entity, SysUserEntity user) {
        entity.setModifier(user.getId());
        if (entity.getId() == null) {
            //新增
            entity.setCreator(user.getId());
            onclickInfoBiz.add(entity);
        } else {
            //修改
            onclickInfoBiz.updateById(entity);
        }
    }

    @Override
    public CommonOnclickInfoEntity getDetailInfo(Integer id) {
        return onclickInfoBiz.queryById(id);
    }

    @Override
    public Map<String, Object> getPageResult(CommonOnclickInfoModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<CommonOnclickInfoEntity> pageResult = onclickInfoBiz.getCommonPageList(params, false);
        return R.ok(pageResult);
    }

    @Override
    public Map deleteBatch(Integer[] ids, SysUserEntity user) {
        String result = this.deleteBatch(ids, user.getId());
        if (R.SUCCESS.equals(result)) {
            return R.ok().msg(commonLanguageProperties.getDeleteSuccess());
        } else {
            return R.fail(result);
        }
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        OnclickInfoServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    /**
     * 处理批量删除
     *
     * @param ids
     * @param userId
     * @return
     */
    private String deleteBatch(Integer[] ids, Integer userId) {
        if (ArrayUtils.isNotEmpty(ids)) {
            UpdateTemplate<CommonOnclickInfoEntity> wrapper = UpdateTemplate.getUpdateTemplate();
            wrapper.in("id", ids);
            CommonOnclickInfoEntity update = new CommonOnclickInfoEntity();
            update.setFlag(0);
            update.setModifier(userId);
            onclickInfoBiz.getMapper().update(update, wrapper);
        }
        return R.SUCCESS;
    }
}
